package org.linlinjava.litemall.wx.web;

import com.alibaba.fastjson.JSONObject;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LitemallBill;
import org.linlinjava.litemall.db.domain.LitemallCustomer;
import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.service.LitemallBillService;
import org.linlinjava.litemall.db.service.LitemallCustomerService;
import org.linlinjava.litemall.db.service.LitemallOrderService;
import org.linlinjava.litemall.db.util.OrderConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 介绍人.邀请人信息(中介会拉人来我们商城.)
 */
@RestController
@RequestMapping("wx/inheritor")
public class WxInheritorController {
    @Autowired
    LitemallCustomerService customerService;
    @Autowired
    LitemallOrderService orderService;
    @Autowired
    LitemallBillService billService;

 /*   @GetMapping("list")
    public Object list(HttpServletRequest request) {
        XxlSsoUser user = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userid = user.getUserid();
        LitemallCustomer crrCustomer = customerService.getUser(Long.parseLong(userid));
        String invitation = crrCustomer.getInvitation();
        LitemallCustomer invitor = customerService.getUser(invitation);
        invitor.secretThis();
        List<LitemallCustomer> invited = customerService.queryByInvitation(crrCustomer.getAccountId());
        for (LitemallCustomer customer : invited) {
            customer.secretThis();
            System.out.println("用户customer>>>>" + customer);
        }
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("invitor", invitor);//邀请人信息
        result.put("invited", invited);//被邀请人信息
        return ResponseUtil.ok(result);
    }*/

// 2020.4.24浩楠发信息:个人中心报错...获取用户个人信息..有人没有邀请函.....
// 解决思路::没有邀请码,邀请人和被邀请人那都是自己


   @GetMapping("list")
    public Object list(HttpServletRequest request) {
        XxlSsoUser user = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userid = user.getUserid();
       //获取用户个人信息的userid 是LONG类型
        LitemallCustomer crrCustomer = customerService.getUser(Long.parseLong(userid));
        //2020.4.24个人中心报错...获取用户个人信息..有人没有邀请函 报错
       //获取邀请函ID//String
        String invitation = crrCustomer.getInvitation();
       // 有 邀请函ID 进入
        if(StringUtils.isNotEmpty(invitation)){
            //邀请函ID..是人的唯一ID//String
            LitemallCustomer invitor = customerService.getUser(invitation);
            invitor.secretThis();
            List<LitemallCustomer> invited = customerService.queryByInvitation(crrCustomer.getAccountId());
            for (LitemallCustomer customer : invited) {
                customer.secretThis();
            }
            Map<String, Object> result = new LinkedHashMap<>();
            //invitor邀请人信息    //invited被邀请人信息
            result.put("invitor", invitor);
            result.put("invited", invited);
            return ResponseUtil.ok(result);
        }else{
            //邀请函ID..是人的唯一ID//String
            LitemallCustomer invitor = crrCustomer;
            invitor.secretThis();
            Map<String, Object> result = new LinkedHashMap<>(2);
            //invitor邀请人信息    //invited被邀请人信息
            result.put("invitor", invitor);
            result.put("invited", invitor);
            return ResponseUtil.ok(result);
        }

    }


    /**
     *  比较当前登录人信息 和传来的用户信息customerId .要不直接比较 userID 和 customerId
     * @param request 登录
     * @param msg   customerId  用户ID
     * @return
     */

    @PostMapping("view")
    public Object view(HttpServletRequest request, @RequestBody String msg) {
        String accountId = JacksonUtil.parseString(msg, "customerId");
        if (StringUtils.isEmpty(accountId)) {
            return ResponseUtil.badArgument();
        }
        XxlSsoUser user = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        LitemallCustomer crrCustomer = customerService.getUser(Long.parseLong(user.getUserid()));
        LitemallCustomer pntCustomer = customerService.getUser(accountId);
        if (pntCustomer == null || crrCustomer == null
                || !pntCustomer.getInvitation().equals(crrCustomer.getAccountId())) {
            return ResponseUtil.unauthz();
        }
        List<LitemallBill> billList = new LinkedList<>();
        List<LitemallOrder> orderList = orderService.list(pntCustomer.getId().intValue(), OrderConstant.COMPLETED, 1, Integer.MAX_VALUE, null, null).getList();
        for (LitemallOrder order : orderList) {
            LitemallBill temp = billService.findByOrder(order.getId());
            if (temp == null) {
                continue;
            }
            LitemallBill bill = new LitemallBill();
            //结算日
            bill.setAddTime(temp.getAddTime());
            bill.setOrderPrice(temp.getOrderPrice());
            bill.setCouponPrice(temp.getCouponPrice());
            bill.setOrderConfirmTime(temp.getOrderConfirmTime());
            bill.setScalperPrice(temp.getScalperPrice());
            bill.setActualPrice(temp.getActualPrice());
            billList.add(bill);
        }
        return ResponseUtil.ok(billList);
    }


}
